﻿#include <iostream>
#include <fstream>
#include <vector>

#include <nlohmann/json.hpp>

#include <Eigen/Core>

using json = nlohmann::json;
using namespace nlohmann::literals;

using namespace Eigen;

// template<typename type_, unsigned rows, unsigned cols>
// class MyVecotr {
// public:
//     // Matrix<type_, rows, cols> matrix_;
//     std::vector<double> vector_;
//     // json::array vector_;

// public:
//     MyVecotr() = default;
//     MyVecotr(std::vector<double> in) : vector_(in) 
//     {
//     }

//     friend void to_json(nlohmann::json& nlohmann_json_j, const MyVecotr& nlohmann_json_t)
//     {
//         nlohmann_json_j["vector"] = nlohmann_json_t.vector_;
// //        nlohmann_json_j["address"] = nlohmann_json_t.address;
// //        nlohmann_json_j["age"] = nlohmann_json_t.age;
//     }

//     friend void from_json(const nlohmann::json& nlohmann_json_j, MyVecotr& nlohmann_json_t)
//     {
//         nlohmann_json_t.vector_ = nlohmann_json_j.at("vector");
// //        nlohmann_json_t.address = nlohmann_json_j.at("address");
// //        nlohmann_json_t.age = nlohmann_json_j.at("age");
//     }

// private:
//     // std::vector< std::vector<double>> matrix_data_;
// };

int load(std::string file_path, json &re) try {
    json l1;
    std::ifstream(file_path) >> l1;
    // json.at("Path").get_to(path);
//    std::cout << l1 << std::endl;
    re = l1;
    return 0;
}
catch (json::exception& e)
{
    std::cout << "deserialization failed: " << e.what() << std::endl;
    return  -1;
}

int main()
{
    // Matrix3d 实质上是 Eigen::Matrix<double, 3, 3>
    // Matrix3d matrix_33 = Matrix3d::Zero(); //初始化为零

    // serialization: person -> json
    json j;
    load("/home/hrl/Desktop/MyLearning/MyCppLearning/nlohmann-learning/json4Eigen/json4Vector/vector.json", j);
    std::cout << "serialization: " << j << std::endl;

    std::vector<double> re;
    j["vector01"].get_to(re);
    std::cout << re.size() << std::endl;

//    try
//    {
//        auto p3 = j3.get<ns::person>();
//    }
//    catch (json::exception& e)
//    {
//        std::cout << "deserialization failed: " << e.what() << std::endl;
//    }
}
